package cn.iocoder.yudao.module.pickbook.controller.admin.voiceactors;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.pickbook.controller.admin.voiceactors.vo.*;
import cn.iocoder.yudao.module.pickbook.dal.dataobject.voiceactors.VoiceActorsDO;
import cn.iocoder.yudao.module.pickbook.service.voiceactors.VoiceActorsService;

@Tag(name = "管理后台 - 配音")
@RestController
@RequestMapping("/pickbook/voice-actors")
@Validated
public class VoiceActorsController {

    @Resource
    private VoiceActorsService voiceActorsService;

    @PostMapping("/create")
    @Operation(summary = "创建配音")
    @PreAuthorize("@ss.hasPermission('pickbook:voice-actors:create')")
    public CommonResult<Long> createVoiceActors(@Valid @RequestBody VoiceActorsSaveReqVO createReqVO) {
        return success(voiceActorsService.createVoiceActors(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新配音")
    @PreAuthorize("@ss.hasPermission('pickbook:voice-actors:update')")
    public CommonResult<Boolean> updateVoiceActors(@Valid @RequestBody VoiceActorsSaveReqVO updateReqVO) {
        voiceActorsService.updateVoiceActors(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除配音")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pickbook:voice-actors:delete')")
    public CommonResult<Boolean> deleteVoiceActors(@RequestParam("id") Long id) {
        voiceActorsService.deleteVoiceActors(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除配音")
                @PreAuthorize("@ss.hasPermission('pickbook:voice-actors:delete')")
    public CommonResult<Boolean> deleteVoiceActorsList(@RequestParam("ids") List<Long> ids) {
        voiceActorsService.deleteVoiceActorsListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得配音")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pickbook:voice-actors:query')")
    public CommonResult<VoiceActorsRespVO> getVoiceActors(@RequestParam("id") Long id) {
        VoiceActorsDO voiceActors = voiceActorsService.getVoiceActors(id);
        return success(BeanUtils.toBean(voiceActors, VoiceActorsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得配音分页")
    @PreAuthorize("@ss.hasPermission('pickbook:voice-actors:query')")
    public CommonResult<PageResult<VoiceActorsRespVO>> getVoiceActorsPage(@Valid VoiceActorsPageReqVO pageReqVO) {
        PageResult<VoiceActorsDO> pageResult = voiceActorsService.getVoiceActorsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, VoiceActorsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出配音 Excel")
    @PreAuthorize("@ss.hasPermission('pickbook:voice-actors:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportVoiceActorsExcel(@Valid VoiceActorsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<VoiceActorsDO> list = voiceActorsService.getVoiceActorsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "配音.xls", "数据", VoiceActorsRespVO.class,
                        BeanUtils.toBean(list, VoiceActorsRespVO.class));
    }

}